package com.app.shop.service.shop.impl;


import com.app.shop.dao.wg.WgCommodityMapper;
import com.app.shop.entity.WgCommodity;
import com.app.shop.entity.WgCommodityExample;
import com.app.shop.entity.query.WgCommodityQuery;
import com.app.shop.service.shop.WgCommodityService;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 商品服务
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("wgCommodityService")
public class WgCommodityServiceImp implements WgCommodityService {

    private static final Logger log = LoggerFactory.getLogger(WgCommodityServiceImp.class);

    @Autowired
    private WgCommodityMapper wgCommodityMapper;

    @Override
    public Page<WgCommodity> getWgCommodityList(WgCommodityQuery wgCommodityQuery) {
        //查询条件
        WgCommodityExample wgCommodityExample = new WgCommodityExample();
        WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
        //商品名称
        if (wgCommodityQuery.getName() != null && wgCommodityQuery.getName().trim().length() > 0) {
            criteria.andNameLike('%' + wgCommodityQuery.getName() + '%');
        }
        //商品上架状态
        if (wgCommodityQuery.getStatus() != null ) {
            criteria.andStatusEqualTo(wgCommodityQuery.getStatus());
        }
        //商品有效失效状态
        if (wgCommodityQuery.getDeleteFlag() != null) {
            criteria.andDeleteFlagEqualTo(wgCommodityQuery.getDeleteFlag());
        }
        //模拟分页查询
        PageHelper.startPage(wgCommodityQuery.getCurrentPage(), wgCommodityQuery.getPageSize());
        return wgCommodityMapper.selectByExample(wgCommodityExample);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editCommodityUp(WgCommodity wgCommodity) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andIdEqualTo(wgCommodity.getId()).andDeleteFlagEqualTo(1).andStatusEqualTo(2);
            WgCommodity sysMenuUp = wgCommodityMapper.selectByPrimaryKeyMore(wgCommodityExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgCommodity.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgCommodity.getModifiedDate());
                sysMenuUp.setStatus(1);
                wgCommodityMapper.updateByExample(sysMenuUp, wgCommodityExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，上架商品必须为上架的有效的商品！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editCommodityDown(WgCommodity wgCommodity) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andIdEqualTo(wgCommodity.getId()).andDeleteFlagEqualTo(1).andStatusEqualTo(1);
            WgCommodity sysMenuUp = wgCommodityMapper.selectByPrimaryKeyMore(wgCommodityExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgCommodity.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgCommodity.getModifiedDate());
                sysMenuUp.setStatus(2);
                wgCommodityMapper.updateByExample(sysMenuUp, wgCommodityExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，下架商品必须为已上架有效的商品！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editCommodityDelete(WgCommodity wgCommodity) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andIdEqualTo(wgCommodity.getId()).andDeleteFlagEqualTo(1);
            WgCommodity sysMenuUp = wgCommodityMapper.selectByPrimaryKeyMore(wgCommodityExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgCommodity.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgCommodity.getModifiedDate());
                sysMenuUp.setStatus(2);
                sysMenuUp.setDeleteFlag(2);
                wgCommodityMapper.updateByExample(sysMenuUp, wgCommodityExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，删除商品必须为有效的商品！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public  SysModel addCommodity(WgCommodity sysCommodity) {
        SysModel sysModel = new SysModel();
        try {
            //查询用户是否存在
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andNameEqualTo(sysCommodity.getName()).andDeleteFlagEqualTo(1);
            //不允许重复菜单
            Long cnt = wgCommodityMapper.countByExample(wgCommodityExample);
            if (cnt < 1) {
                //商品默认下架有效状态
                sysCommodity.setStatus(2);
                sysCommodity.setDeleteFlag(1);
                sysCommodity.setType(1);
                sysCommodity.setSaleNum(0);
                wgCommodityMapper.insert(sysCommodity);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("添加失败，存在有效同名商品，不允许重复添加！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品添加失败，修改过程发生异常请重试！");
            log.error("商品添加过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public synchronized SysModel editCommodity(WgCommodity wgCommodity) {
        SysModel sysModel = new SysModel();
        try {
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andDeleteFlagEqualTo(1).andNameEqualTo(wgCommodity.getName()).andIdNotEqualTo(wgCommodity.getId());
            Long cnt = wgCommodityMapper.countByExample(wgCommodityExample);
            if (cnt < 1) {
                wgCommodityExample = new WgCommodityExample();
                criteria = wgCommodityExample.createCriteria();
                criteria.andDeleteFlagEqualTo(1).andIdEqualTo(wgCommodity.getId());
                WgCommodity wgCommodityUp =wgCommodityMapper.selectByPrimaryKeyMore(wgCommodityExample);
                if(wgCommodityUp!=null) {
                    wgCommodityUp.setModifiedUserId(wgCommodity.getModifiedUserId());
                    wgCommodityUp.setModifiedDate(wgCommodity.getModifiedDate());
                    wgCommodityUp.setName(wgCommodity.getName());
                    wgCommodityUp.setPrice(wgCommodity.getPrice());
                    wgCommodityUp.setMarkerDesc(wgCommodity.getServiceDesc());
                    wgCommodityUp.setStockNum(wgCommodity.getStockNum());
                    wgCommodityUp.setPriceOld(wgCommodity.getPrice());
                    wgCommodityUp.setServiceDesc(wgCommodity.getMarkerDesc());
                    wgCommodityUp.setPictureId(wgCommodity.getPictureId());
                    wgCommodityMapper.updateByExample(wgCommodityUp, wgCommodityExample);
                    sysModel.setSuccess(true);
                }else {
                    sysModel.setSuccess(false);
                    sysModel.setMsg("商品修改失败，当前商品已经被删除，不允许再次修改！");
                }
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，可能存在有效同名商品！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editCommodityArticle(WgCommodity wgCommodity) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgCommodityExample wgCommodityExample = new WgCommodityExample();
            WgCommodityExample.Criteria criteria = wgCommodityExample.createCriteria();
            criteria.andDeleteFlagEqualTo(1).andIdEqualTo(wgCommodity.getId());
            WgCommodity wgCommodityUp = wgCommodityMapper.selectByPrimaryKeyMore(wgCommodityExample);
            if (wgCommodityUp != null) {
                wgCommodityUp.setModifiedUserId(wgCommodity.getModifiedUserId());
                wgCommodityUp.setModifiedDate(wgCommodity.getModifiedDate());
                wgCommodityUp.setDescription(wgCommodity.getDescription());
                wgCommodityMapper.updateByExampleWithBLOBs(wgCommodityUp, wgCommodityExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("商品修改失败，可能当前商品已经被删除,只能修改有效商品图文！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("商品修改失败，修改过程发生异常请重试！");
            log.error("商品修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    public WgCommodity getCommodityById(WgCommodity wgCommodity) {
        return wgCommodityMapper.selectByPrimaryKey(wgCommodity.getId());
    }

}
